package mrusanov.season

import java.util.{Calendar, Date}
import mrusanov.tournament.MatchInfo
import mrusanov.ai.AIBattlePlayer

class SeasonDay(day:Date, season: WorldSeason) {

  val battlePlayer = new AIBattlePlayer

  def playDay() {
    // relax
    season.nationSeasons.foreach(_.allTeams.foreach(_.relaxOneDay(day)))

    // play games
    season.dateGames(day).filterNot(_.isOver).foreach(playGame(_))

    // calculate conflicts and friendships if this is the last day of month
    if (isLastDayOfMonth(day)) {
      season.nationSeasons.foreach(_.allTeams.foreach(_.calculatePlayersRelations()))
    }

    season.world.refreshClubRating()
  }



  private def playGame(game:MatchInfo) {
    battlePlayer.playBattle(game)
  }

  private def isLastDayOfMonth(date:Date):Boolean = {
    val calendar = Calendar.getInstance()
    calendar.setTime(date)
    val month = calendar.get(Calendar.MONTH)
    calendar.add(Calendar.DAY_OF_MONTH, 1)
    calendar.get(Calendar.MONTH) != month
  }
}
